package com.cloud.wyf.Controller;

import com.alibaba.fastjson2.JSONObject;
import com.cloud.wyf.Entity.*;
import com.cloud.wyf.Service.UserService;
import com.cloud.wyf.Util.GetTokenUtil;
import com.cloud.wyf.Util.OutUtil;
import com.cloud.wyf.Util.POIUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/data")
public class DataController {

    @Autowired
    UserService us;

    @RequestMapping("/importMi")
    public void importDataMi(MultipartFile file, HttpServletRequest request, HttpServletResponse response) throws IOException {
        List<Middel> list= POIUtil.excelLeader(file);
        int i = us.MultiAddMi(list);
        if(i>0) response.getWriter().write(JSONObject.toJSONString(Message.success("success insert")));
        else  response.getWriter().write(JSONObject.toJSONString(Message.failure("failure insert")));
    }

    @RequestMapping("/importOr")
    public void importDataOr(MultipartFile file, HttpServletRequest request, HttpServletResponse response) throws IOException {
        List<Ordinary> list= POIUtil.excelOrdinary(file);
        int i = us.MultiAddOr(list);
        if(i>0) response.getWriter().write(JSONObject.toJSONString(Message.success("success insert")));
        else  response.getWriter().write(JSONObject.toJSONString(Message.failure("failure insert")));
    }

    /**
     * 管理员上传述职报告不需要审核
     * @param file
     * @param request
     * @param response
     * @throws IOException
     */
    @RequestMapping("/importS")
    public void importDataS(MultipartFile file, HttpServletRequest request, HttpServletResponse response) throws IOException {
        try {
            String path="E:\\reports\\" + file.getOriginalFilename();
            File file1 = new File(path);
            if (!file1.exists()) {
                file.transferTo(file1);
            }
            String uid = request.getParameter("uid");
            int i = us.uploadS(path, uid,1);
            if(i>0) response.getWriter().write(JSONObject.toJSONString(Message.success("success upload")));
            else response.getWriter().write(JSONObject.toJSONString(Message.failure("failure upload")));
        }catch (Exception e){
            response.getWriter().write(JSONObject.toJSONString(Message.failure("failure upload")));
        }
    }

    /**
     * 中层干部上传述职报告
     * @param file
     * @return
     */
    @RequestMapping("/import")
    public void uploadFile(MultipartFile file,HttpServletRequest request) throws IOException {
        String uid=request.getParameter("uid");
        String path="E:\\uploadFile\\" + file.getOriginalFilename();
        File file1 = new File(path);
        if (!file1.exists()) {
            file.transferTo(file1);
        }
       us.uploadS(path, uid, 2);
    }

    /**
     * 上传领导班子报告
     * @param file
     * @param request
     * @throws IOException
     */
    @RequestMapping("/importL")
    public void uploadFileL(MultipartFile file,HttpServletRequest request) throws IOException {
        String uid=request.getParameter("uid");
        User user = us.selectUserById(uid);
        int role=user.getRole();
        String path="E:\\uploadFile\\" + file.getOriginalFilename();
        File file1 = new File(path);
        if (!file1.exists()) {
            file.transferTo(file1);
        }
        if(role==1){
            Leader leader = us.selectLeaderById(uid);
            if(leader!=null) {
                LeaderShip leaderShip=new LeaderShip(leader.getPart(),path,new Date(System.currentTimeMillis()),leader.getUid(),2);
                us.updateLeaderShip(leaderShip);
            }
        }
        else if(role==2){
            Middel middel = us.selectMidById(uid);
            if(middel!=null) {
                LeaderShip leaderShip = new LeaderShip(middel.getPart(), path, new Date(System.currentTimeMillis()), middel.getUid(), 2);
                us.updateLeaderShip(leaderShip);
            }
        }
    }

    @RequestMapping("/importUpdateM")
    public void MultUpdateTemplate(HttpServletRequest request,HttpServletResponse response){
        OutUtil.exportExcel1(response);
    }

    @RequestMapping("/importUpdate")
    public String MultUpdate(MultipartFile file,HttpServletRequest request){
        String uid = request.getParameter("uid");
        if(!"1".equals(uid)) return JSONObject.toJSONString(Message.failure("you have no power"));
        List<Middel> list = POIUtil.excelMiddel(file);
        int i = us.updateInfo(list);
        if(i>0) return JSONObject.toJSONString(Message.success("success"));
        else return JSONObject.toJSONString(Message.failure("failure"));
    }
}
